const eventNames = [
    'PageChange',
    'LoginSuccess',
    'CodeError',
    'ServerError',
    'ResetStore',
    'CancleInfo',
    'AddSuccess',
    'UploadSuccess',
    'UpdateSuccess',
    'DeleteSuccess',
    'ShowNotification',
    'HandleError',
] as const;
type EventNames = (typeof eventNames)[number]

class EventBus {
    private eventMap: Record<EventNames, Set<Function>> = {
        PageChange: new Set(),
        LoginSuccess: new Set(),
        CodeError: new Set(),
        ServerError: new Set(),
        ResetStore: new Set(),
        CancleInfo: new Set(),
        AddSuccess: new Set(),
        UploadSuccess: new Set(),
        UpdateSuccess: new Set(),
        DeleteSuccess: new Set(),
        ShowNotification: new Set(),
        HandleError: new Set(),
    }
    on(eventName: EventNames, fn: Function) {
        this.eventMap[eventName].add(fn)
    }
    emit(eventName: EventNames, ...args: any[]) {
        this.eventMap[eventName].forEach(fn => {
            fn(...args)
        })
    }
}

export default new EventBus()